import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import java.io.*;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;

import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpLoginException;
 
public class Gui extends JFrame implements ActionListener
{
	private static final long serialVersionUID = -5740956600586131361L;
	
	private FtpAction ftpa=new FtpAction();
	
	private List list = new List();

	private JPanel FtpClientFrame = new JPanel(new BorderLayout());
	private JPanel FtpClientFrameOne = new JPanel(new FlowLayout(FlowLayout.LEFT));
	private JPanel FtpClientFrameTwo = new JPanel(new GridLayout(1, 8));
	private JPanel FtpClientFrameThree = new JPanel(new GridLayout(2, 1));
	private JPanel FtpClientFrameFour = new JPanel(new GridLayout(1, 2));
	private JPanel FtpClientFrameFive = new JPanel(new GridLayout(2, 1));
	private JPanel FtpClientFrameSix = new JPanel(new GridLayout(1, 3));
	private JPanel FtpClientFrameSeven = new JPanel(new GridLayout(1, 3));
	
/* connection or break button 
 * 
 */
	private JButton linkButton = new JButton("Link");
	private JButton breakButton = new JButton("Break");
	
/*get put button
 * 
 */
	
	private JButton getButton = new JButton("get");
	protected JProgressBar m_progress = new JProgressBar();
//	protected JProgressBar m_progress = new JProgressBar();
	private JButton putButton = new JButton("put");
	
/* connection status
 * 
 */
	private JLabel statusLabel = new JLabel();
/*login user
 * 
 */
	private JLabel urlLabel = new JLabel("Ftp URL:");
	private JLabel usernameLabel = new JLabel("username:");
	private JLabel passwordLabel = new JLabel("password:");
	private JLabel portLabel = new JLabel("port:");
	private JTextField urlTextField = new JTextField(10);
	private JTextField usernameTextField = new JTextField("anonymous");
	protected JPasswordField passwordTextField = new JPasswordField(10);
	private JTextField portTextField = new JTextField(10);
	
/*show file
 * 
 */
	private JLabel localFileLabel = new JLabel("localFile:");
	private JButton chooseFileButton = new JButton("Choose File");
	private JLabel remoteFileLabel = new JLabel("remoteFile:");
	private JTextField localTextField = new JTextField(10);
	private JTextField remoteTextField = new JTextField(10);
	
/*local and distance scroll
 * 
 */
	DefaultListModel modelList = new DefaultListModel();
	private JList localList = new JList();
	private JList distanceList = new JList();
	JScrollPane localScrollPane = new JScrollPane(localList);
	JScrollPane distanceScrollPane = new JScrollPane(distanceList);

/*choose flie
 * 
 */	
	protected JFileChooser fileChooser = new JFileChooser();

	protected String localFileName;
	protected String remoteFileName;
	protected JTextArea monitorTextArea = new JTextArea(5, 20);

/*本地、远程目录树
	private JTree localTree;
	private JTree ServerTree;
	*/

	public Gui()
	{
		FtpClientFrameOne.add(linkButton);
		FtpClientFrameOne.add(breakButton);
		FtpClientFrameOne.add(statusLabel);

		FtpClientFrameTwo.add(urlLabel);
		FtpClientFrameTwo.add(urlTextField);
		FtpClientFrameTwo.add(usernameLabel);
		FtpClientFrameTwo.add(usernameTextField);
		FtpClientFrameTwo.add(passwordLabel);
		FtpClientFrameTwo.add(passwordTextField);
		FtpClientFrameTwo.add(portLabel);
		FtpClientFrameTwo.add(portTextField);

		FtpClientFrameThree.add(FtpClientFrameTwo);
		FtpClientFrameThree.add(FtpClientFrameOne);

		FtpClientFrameFour.add(localScrollPane);
		FtpClientFrameFour.add(list);
		
		FtpClientFrameSix.add(getButton);
 
		
		FtpClientFrameSix.add(m_progress);
		FtpClientFrameSix.add(putButton);
 
		
		FtpClientFrameSeven.add(localFileLabel);
		FtpClientFrameSeven.add(localTextField);
		FtpClientFrameSeven.add(chooseFileButton);		
		FtpClientFrameSeven.add(remoteFileLabel);
		FtpClientFrameSeven.add(remoteTextField);
		
		FtpClientFrameFive.add(FtpClientFrameSix);
		FtpClientFrameFive.add(FtpClientFrameSeven);

		FtpClientFrame.add(FtpClientFrameThree, "North");
		FtpClientFrame.add(FtpClientFrameFour, "Center");
		FtpClientFrame.add(FtpClientFrameFive, "South");
		
	    m_progress.setStringPainted(true);
	    m_progress.setBorder(new BevelBorder(BevelBorder.LOWERED, Color.white,Color.gray));
	    m_progress.setMinimum(0); 

		localFileName = localTextField.getText();
	    
		setContentPane(FtpClientFrame);
		setTitle("FunFtp");
		setSize(600, 500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);

/*choose file
 * 
 */		
		ActionListener lst = new ActionListener() {
	        public void actionPerformed(ActionEvent e) {
	          if (fileChooser.showSaveDialog(Gui.this) != JFileChooser.APPROVE_OPTION)
	            return;
	          File f = fileChooser.getSelectedFile();
	          localTextField.setText(f.getPath());
	        }
	      };
	      chooseFileButton.addActionListener(lst);
	      chooseFileButton.setMnemonic('f');
/*choose file
 * 
 */	      
	      

		linkButton.addActionListener(this);
		breakButton.addActionListener(this);
	}
	public void actionPerformed(ActionEvent evt)
	{
		Object source = evt.getSource();
		if (source == linkButton) {
 //connection Ftp server
		FtpClient ftp=ftpa.getFtp();
		try {
			if (ftp != null)
			ftp.closeServer();
			statusLabel.setText("connectioning it,plesea waiting...");
			ftp = new FtpClient(urlTextField.getText());
			String password = new String(passwordTextField.getPassword());
			ftp.login(usernameTextField.getText(),password);
			ftp.binary();
		}
		catch (FtpLoginException e) {
			JOptionPane.showMessageDialog(null, "Login Failure!!!");
			e.printStackTrace();
		}
		catch (IOException e) {
			JOptionPane.showMessageDialog(null,
			urlTextField.getText() + "Connection Failure!!!");
			e.printStackTrace();
		}
		catch (SecurityException e) {
			JOptionPane.showMessageDialog(null, "No Purview!!!");
			e.printStackTrace();
		}
		if (urlTextField.getText().equals(""))
		JOptionPane.showMessageDialog(null, "FtpServer url can't empty!");
		else if (usernameTextField.getText().equals(""))
		JOptionPane.showMessageDialog(null, "Username can't empty!");
		else if (passwordTextField.getPassword().equals(""))
		JOptionPane.showMessageDialog(null, "Password can't empty!");
       else 
    	statusLabel.setText("Link to Ftp:" + urlTextField.getText());
		new FtpAction().loadList(ftp);
	}
	if (source == breakButton) {
		System.exit(0);
	}
	}
}
